package com.design.mode.command;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-04-12 11:02:05
 */
public class Invoker {

    private final Command[] offs;
    private final Command[] ons;

    private Command undo;
    private static final Integer SIZE = 5;

    public Invoker() {
        ons = new Command[SIZE];
        offs = new Command[SIZE];

        for (int i = 0; i < SIZE; i++) {
            ons[i] = new NoCommand();
            offs[i] = new NoCommand();
        }
    }

    public void setCommand(int i, Command on, Command off) {
        ons[i] = on;
        offs[i] = off;
    }

    public void onButton(int i) {
        ons[i].execute();
        undo = ons[i];
    }

    public void offButton(int i) {
        offs[i].execute();
        undo = offs[i];
    }

    public void unButton() {
        undo.undo();
    }
}
